﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Studio77.RuneMaster.Entities;
using System.Windows.Media.Imaging;
using Studio77.RuneMaster.Core;
using Studio77.RuneMaster.Pages;

namespace Studio77.RuneMaster.Views
{
    public partial class ToolItemInGame : UserControl
    {
        static object syncObj = new object();

        public ToolType Type { get; set; }
        public int Count { get; set; }

        public ToolItemInGame()
        {
            InitializeComponent();
            this.MouseLeftButtonDown += ToolItemInGame_MouseLeftButtonDown;
        }

        void ToolItemInGame_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            lock (syncObj)
            {
                if (Count > 0)
                {
                    //如果是重排道具特殊处理下
                    if (Type == ToolType.ReArrange || Type == ToolType.CreateNewSpriteRune || Type == ToolType.CollectSprite)
                    {
                        SoundManager.PlayEffect(SoundManager.skill);

                        for (int i = 0; i < 4; i++)
                        {
                            if (StageManager.Instance.CurrentToolSlotInfo[i] != null && StageManager.Instance.CurrentToolSlotInfo[i].Type == Type)
                            {
                                StageManager.Instance.CurrentToolSlotInfo[i].LeftCount--;
                                break;
                            }
                        }

                        //做个动画
                        StoryboardClick.Begin();
                        GamePage.Instance.UpdateToolUI();

                        switch (Type)
                        {
                            case ToolType.ReArrange:
                                //重排
                                GamePage.Instance.ReArrange();
                                break;
                            case ToolType.CollectSprite:
                                //收集所有的精神力道具
                                GamePage.Instance.CollectAllSpirit();
                                break;
                            case ToolType.CreateNewSpriteRune:
                                //创建精神力符文（2个）
                                GamePage.Instance.CreateNewSpiritRune();
                                break;
                        }
                    }
                    else
                    {

                        //使用道具
                        bool isUse = false;

                        for (int useSlot = 0; useSlot < StageManager.Instance.CurrentUsedTool.Count; useSlot++)
                        {
                            if (StageManager.Instance.CurrentUsedTool[useSlot] == ToolType.NoTool)
                            {
                                //可以使用
                                isUse = true;
                                StageManager.Instance.CurrentUsedTool[useSlot] = Type;
                                //减少当前道具数量
                                for (int i = 0; i < 4; i++)
                                {
                                    if (StageManager.Instance.CurrentToolSlotInfo[i] != null && StageManager.Instance.CurrentToolSlotInfo[i].Type == Type)
                                    {
                                        StageManager.Instance.CurrentToolSlotInfo[i].LeftCount--;
                                        break;
                                    }
                                }
                                break;
                            }
                            else if (StageManager.Instance.CurrentUsedTool[useSlot] == Type)
                            {
                                isUse = false;
                                break;
                            }
                        }

                        if (isUse)
                        {
                            //做个动画
                            StoryboardClick.Begin();
                            GamePage.Instance.UpdateToolUI();
                            GamePage.Instance.UpdateUsedToolUI();

                            SoundManager.PlayEffect(SoundManager.useTool);
                        }
                    }
                }
            }
        }

        public void SetTool(ToolType type)
        {
            string uri = string.Empty;
            switch (type)
            {
                case ToolType.Add500:
                    uri = "/Images/add500.png";
                    break;
                case ToolType.CollectSprite:
                    uri = "/Images/CollectSprite.png";
                    break;
                case ToolType.CreateNewSpriteRune:
                    uri = "/Images/AddSpriteRune.png";
                    break;
                case ToolType.ReArrange:
                    uri = "/Images/ReArrange.png";
                    break;
                case ToolType.ScoreMutil2:
                    uri = "/Images/score2.png";
                    break;
                case ToolType.SpriteMutil2:
                    uri = "/Images/Sprite2.png";
                    break;
            }

            BitmapImage bitmap = new BitmapImage(new Uri(uri, UriKind.RelativeOrAbsolute));
            imgTool.Source = bitmap;
            Type = type;
        }

        public void SetToolCount(int count)
        {
            Count = count;
            this.txtCount.Text = count.ToString();

            if (count == 0)
            {
                this.imgTool.Opacity = 0.3;
            }
            else
            {
                this.imgTool.Opacity = 1;
            }
        }
    }
}
